package jsyf.oa.web.email;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.email.SentEmailFacade;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by LT on 2018/3/7.
 */
@SuppressWarnings("all")
@Api(value = "已发送邮件接口", description = "已发送邮件功能")
@Controller
@RequestMapping("/sentEmail")
public class SentEmailController {

    @Resource
    private SentEmailFacade sentEmailFacade;

    @ApiOperation(
            value = "已发送邮件列表查询",
            notes = "已发送邮件列表查询",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{requestPage: \"起始页\", pageSize:\"每页大小\", createUserId: \"创建邮件用户id\",searchKey: \"主题的部分字段\"}", dataType = "String", paramType = "body")
    @ResponseBody
    @RequestMapping(value = "/findList.json", method = RequestMethod.POST)
    public void findList(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        JSONObject records;
        String requestPage;
        String pageSize;
        String seaarchKey=jsonParams.getString("searchKey");
        if (jsonParams.containsKey("requestPage") && jsonParams.containsKey("pageSize")) {
            requestPage = jsonParams.getString("requestPage");
            pageSize = jsonParams.getString("pageSize");
            records = this.sentEmailFacade.findSentList(jsonParams.getString("createUserId"),seaarchKey,Integer.parseInt(requestPage), Integer.parseInt(pageSize));
        } else {
            requestPage = "1";
            pageSize = Integer.MAX_VALUE + "";
            records = this.sentEmailFacade.findSentList(jsonParams.getString("createUserId"),seaarchKey, Integer.parseInt(requestPage), Integer.parseInt(pageSize));
        }
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }

    @ApiOperation(
            value = "已发送邮件详细查询",
            notes = "已发送邮件详细查询",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{id: \"邮件id\"}", dataType = "String", paramType = "body")
    @ResponseBody
    @RequestMapping(value = "/getEmail.json", method = RequestMethod.POST)
    public void getEmail(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {

        JSONObject records = sentEmailFacade.findSentById(jsonData);
        if (records != null) {
            ResponseUtils.putJsonResponse(response,  BaseUtils.succsessObjTimeRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }

    @ApiOperation(
            value = "删除已发送邮件",
            notes = "删除已发送邮件",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{id: \"邮件id\"}", dataType = "String", paramType = "body")
    @ResponseBody
    @RequestMapping(value = "/removeSentEmail.json", method = RequestMethod.POST)
    public void removeSentEmail(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        Boolean records = sentEmailFacade.removeSentById(jsonParams.getString("id"));

        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(records.toString()));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }



}


